package systemfnomabss


//abstract class Binding 


class NameBindingContext[B,Name](private val bins:List[(Name,B)]) {
  
  def + (n: Name, b: B) = create( (n,b):: bins)
  def apply(i: Int) : Option[B] = if (!(0<=i && i < bins.length)) 
                                   None else Some(bins(i)._2)
  def apply(n:Name) : Option[B] = { val filtered = bins.filter( {case (bound,b) => (n == bound)  })
                                    if (filtered.length == 1) Some(filtered(0)._2) else None }
  def isNameBound(n: Name): Boolean =  bins exists { case (s, b)  => (s == n) }
  
  def getIndexOfName(n: Name) :Int = getIndexOfName(n, bins)
  private def getIndexOfName(n: Name,bins: List[(Name,B)]) : Int = {
    val index = bins.map {case (n, _) => n} indexOf(n)
    if(index == -1) throw new Exception(n +" is unbound variable") else index
  }
  
  
  def create(bindings:List[(Name,B)]) = new NameBindingContext[B,Name](bindings)
  override def toString = bins.toString
}



